//
//  BLStatusFrame.m
//  新浪微博
//
//  Created by hbl on 16/2/22.
//  Copyright © 2016年 hbl. All rights reserved.
//

#import "BLStatusFrame.h"
#import "BLStatus.h"
#import "BLUser.h"
#import "BLPhotosView.h"
#define cellBorder 8
#define cellRowBorder 3
#define nameFont [UIFont systemFontOfSize:15]
#define timeFont [UIFont systemFontOfSize:12]
#define sourceFont [UIFont systemFontOfSize:12]
#define contentFont [UIFont systemFontOfSize:15]
#define retweetnameFont [UIFont systemFontOfSize:14]
#define retweetcontentTextFont [UIFont systemFontOfSize:14]
@implementation BLStatusFrame
- (void)setStatus:(BLStatus *)status
{
    _status = status;
    //cell高度
    CGFloat cellW = [UIScreen mainScreen].bounds.size.width;
    
    //topView
    CGFloat topViewW = cellW;
    CGFloat topViewX = 0;
    CGFloat topViewY = 0;
    
    //iconView
    CGFloat iconViewX = cellBorder;
    CGFloat iconViewY = cellBorder;
    CGFloat iconViewWH = 35;
    self.iconViewF = CGRectMake(iconViewX, iconViewY, iconViewWH, iconViewWH);
    
    //name
    CGFloat nameX = CGRectGetMaxX(self.iconViewF) + cellBorder;
    CGFloat nameY = cellBorder;
    NSDictionary *attr = [NSDictionary dictionary];
    attr = @{NSFontAttributeName:nameFont};
    CGRect nameSize = [self.status.user.name boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, iconViewWH) options:NSStringDrawingUsesLineFragmentOrigin attributes:attr context:nil];
    CGFloat nameH = nameSize.size.height;
    CGFloat nameW = nameSize.size.width;
    self.nameLabelF = CGRectMake(nameX, nameY, nameW, nameH);
    
    //VIP
    if (self.status.user.verified_type > -1) {
        CGFloat vipX = CGRectGetMaxX(self.nameLabelF) + cellBorder;
        CGFloat vipY = cellBorder;
        CGFloat vipW = 14;
        CGFloat vipH = nameH;
        self.vipViewF = CGRectMake(vipX, vipY, vipW, vipH);
    }
    
    //time
    CGFloat timeX = nameX;
    CGFloat timeY = CGRectGetMaxY(self.nameLabelF) + cellBorder;
    NSDictionary *timeAttr = [NSDictionary dictionary];
    timeAttr = @{NSFontAttributeName:sourceFont};
    CGRect timeSize = [self.status.created_at boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, iconViewWH) options:NSStringDrawingUsesLineFragmentOrigin attributes:timeAttr context:nil];
    CGFloat timeH = timeSize.size.height;
    CGFloat timeW = timeSize.size.width;
    self.timeLabelF = CGRectMake(timeX, timeY, timeW, timeH);
    
    //source
    CGFloat sourceX = CGRectGetMaxX(self.timeLabelF) + cellBorder;
    CGFloat sourceY = timeY;
    NSDictionary *sourceAttr = [NSDictionary dictionary];
    sourceAttr = @{NSFontAttributeName:sourceFont};
    CGRect sourceSize = [self.status.source boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, iconViewWH) options:NSStringDrawingUsesLineFragmentOrigin attributes:sourceAttr context:nil];
    CGFloat sourceW = sourceSize.size.width;
    CGFloat sourceH = sourceSize.size.height;
    self.sourceLabelF = CGRectMake(sourceX, sourceY, sourceW, sourceH);
    
    //contentText
    CGFloat contentTextX = cellBorder;
    CGFloat contentTextY = CGRectGetMaxY(self.iconViewF) + cellBorder * 2;
    NSDictionary *contentTextAttr = [NSDictionary dictionary];
    contentTextAttr = @{NSFontAttributeName:contentFont};
    CGRect contentTextRect = [self.status.text boundingRectWithSize:CGSizeMake(cellW - 2*cellBorder, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:contentTextAttr context:nil];
    CGFloat contentTextW = contentTextRect.size.width;
    CGFloat contentTextH = contentTextRect.size.height;
    self.contentTextLabelF = CGRectMake(contentTextX, contentTextY, contentTextW, contentTextH);
    
    //配图
    if (self.status.pic_urls.count) {
        CGFloat thumbnail_picX = iconViewX;
        CGFloat thumbnail_picY = CGRectGetMaxY(self.contentTextLabelF) + cellBorder;
        CGSize thumbnail_picSize = [BLPhotosView photosViewSizeWithPhotosCount:(int)self.status.pic_urls.count];
        self.photoViewF = CGRectMake(thumbnail_picX, thumbnail_picY, thumbnail_picSize.width, thumbnail_picSize.height);
    }
    
   //被转发部分-------------------
    if (self.status.retweeted_status) {
        BLStatus *retweetStatus = self.status.retweeted_status;
        //被转发微博主View
        CGFloat retweetViewX = cellBorder;
        CGFloat retweetViewY = CGRectGetMaxY(self.contentTextLabelF) + cellBorder;
        CGFloat retweetViewW = cellW - 2*cellBorder;
        
        //retweetname
        CGFloat retweetnameX = cellBorder;
        CGFloat retweetnameY = cellBorder * 2;
        NSDictionary *retweetnameAttr = [NSDictionary dictionary];
        retweetnameAttr = @{NSFontAttributeName:retweetnameFont};
        CGRect retweetnameRect = [[NSString stringWithFormat:@"@%@",retweetStatus.user.name] boundingRectWithSize:CGSizeMake(CGFLOAT_MAX, iconViewWH) options:NSStringDrawingUsesLineFragmentOrigin attributes:retweetnameAttr context:nil];
        CGFloat retweetnameW = retweetnameRect.size.width;
        CGFloat retweetnameH = retweetnameRect.size.height;
        self.retweetnameLabelF = CGRectMake(retweetnameX, retweetnameY, retweetnameW, retweetnameH);
        
        //retweetcontentText
        CGFloat retweetcontentTextX = retweetnameX;
        CGFloat retweetcontentTextY = CGRectGetMaxY(self.retweetnameLabelF) + cellBorder;
        NSDictionary *retweetcontentTextAttr = [NSDictionary dictionary];
        retweetcontentTextAttr = @{NSFontAttributeName:retweetcontentTextFont};
        CGRect retweetcontentRect = [retweetStatus.text boundingRectWithSize:CGSizeMake(cellW - 4*cellBorder, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:retweetcontentTextAttr context:nil];
        CGFloat retweetcontentTextW = retweetcontentRect.size.width;
        CGFloat retweetcontentTextH = retweetcontentRect.size.height;
        self.retweetcontentTextLabelF = CGRectMake(retweetcontentTextX, retweetcontentTextY, retweetcontentTextW, retweetcontentTextH);
        
        //转发配图
        if (retweetStatus.pic_urls.count) {
            CGFloat rthumbnail_picX = retweetnameX;
            CGFloat rthumbnail_picY = CGRectGetMaxY(self.retweetcontentTextLabelF) + cellBorder;
            CGSize rthumbnail_picSize = [BLPhotosView photosViewSizeWithPhotosCount:(int)retweetStatus.pic_urls.count];
            self.retweetphotoViewF = CGRectMake(rthumbnail_picX, rthumbnail_picY, rthumbnail_picSize.width, rthumbnail_picSize.height);
        }
        
         //Cell高度
        if (retweetStatus.thumbnail_pic) {
            CGFloat retweetViewH = CGRectGetMaxY(self.retweetphotoViewF)  + cellBorder;
            self.retweetViewF = CGRectMake(retweetViewX, retweetViewY, retweetViewW, retweetViewH);
            
            CGFloat topViewH = CGRectGetMaxY(self.retweetViewF) + cellBorder;
            self.topViewF = CGRectMake(topViewX, topViewY, topViewW, topViewH );

            
            //self.cellHight = CGRectGetMaxY(self.topViewF) + cellRowBorder ;
        }else{
            CGFloat retweetViewH = CGRectGetMaxY(self.retweetcontentTextLabelF)  + cellBorder;
            self.retweetViewF = CGRectMake(retweetViewX, retweetViewY, retweetViewW, retweetViewH);
            
            CGFloat topViewH = CGRectGetMaxY(self.retweetViewF) + cellBorder;
            self.topViewF = CGRectMake(topViewX, topViewY, topViewW, topViewH );
            
            //self.cellHight = CGRectGetMaxY(self.topViewF) + cellRowBorder;
        }


    }else{
         //Cell高度
        if (self.status.thumbnail_pic) {
            CGFloat topViewH = CGRectGetMaxY(self.photoViewF);
            self.topViewF = CGRectMake(topViewX, topViewY, topViewW, topViewH + cellBorder);
           // self.cellHight = self.topViewF.size.height + cellRowBorder;
        }else{
            CGFloat topViewH = CGRectGetMaxY(self.contentTextLabelF);
            self.topViewF = CGRectMake(topViewX, topViewY, topViewW, topViewH + cellBorder);
            //self.cellHight = self.topViewF.size.height + cellRowBorder;
        }

    }
   
    
    //toolBar
    CGFloat toolBarX = 0;
    CGFloat toolBarY = CGRectGetMaxY(self.topViewF);
    CGFloat toolBarW = cellW;
    CGFloat toolBarH = 35;
    self.toolBarViewF = CGRectMake(toolBarX, toolBarY, toolBarW, toolBarH);
    
    //cellHight
    self.cellHight = CGRectGetMaxY(self.toolBarViewF) + cellRowBorder;
}
@end
